home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 May / com_0505_1.iso / opensource / top10 / amc_install.exe / {app} / Scripts / Culturalia+IMDB.ifs < prev    next >
Encoding:
Text File  |  2004-10-01  |  33.2 KB  |  1,037 lines

  1. // GETINFO SCRIPTING
  2. // Culturalia+IMDB
  3.  
  4. (***************************************************
  5. *  Script merged by Jose Miguel Folgueira, based   *
  6. *  on a similar script merged by Antoine Potten    *
  7. *                                                  *
  8. *  Movie importation script for:                   *
  9. *    Culturalia, http://www.culturalianet.com      *
  10. *                                                  *
  11. *  Original version made by David Arenillas        *
  12. *  New version made by Antoine Potten              *
  13. *  Contributors:                                   *
  14. *    Jose Miguel Folgueira                         *
  15. *    RedDwarf                                      *     
  16. *    Hades666                                      *
  17. *    KaBeCi                                        *
  18. *    Pollopolea                                    *
  19. *                                                  *
  20. *  Thanks to Culturalia's webmaster for his help   *
  21. *  and for providing more direct access to his     *
  22. *  database                                        *
  23. *                                                  *
  24. *  Movie importation script for:                   *
  25. *      IMDB (US), http://us.imdb.com               *
  26. *                                                  *
  27. *  TransformTitle function from IMDB (US) Script   *
  28. *  (c) 2002-2004 Antoine Potten                    *
  29. *                          software@antp.be        *
  30. *                                                  *
  31. *  (c) 2002 Antoine Potten    antoine@buypin.com   *
  32. *  Contributors :                                  *
  33. *    Danny Falkov                                  *
  34. *    Kai Blankenhorn                               *
  35. *    lboregard                                     *
  36. *    Ork <ork@everydayangels.net>                  *
  37. *    Trekkie <Asimov@hotmail.com>                  *
  38. *    Youri Heijnen                                 *
  39. *                                                  *
  40. *  For use with Ant Movie Catalog 3.4.x            *
  41. *  www.ant.be.tf/moviecatalog ╖╖╖ www.buypin.com   *
  42. *                                                  *
  43. *  The source code of the script can be used in    *
  44. *  another program only if full credits to         *
  45. *  script author and a link to Ant Movie Catalog   *
  46. *  website are given in the About box or in        *
  47. *  the documentation of the program                *
  48. *                                                  *
  49. ***************************************************)
  50.  
  51. program Culturalia_IMDb;
  52. const
  53.   BaseURLCulturalia = 'http://www.culturalianet.com/bus/catalogo.php';
  54.   UseLongestDescIMDB = False; // If set to False shortest description available will be imported, faster since taken from main page
  55.  
  56.   // Set the following constants to True to import field, or False to skip field (fiels to import from IMDB). By default, only the fields not available at  Culturalia are set to True.
  57.   // Pon las siguientes constantes a True para importar campo o False para no hacerlo (campos a importar de IMDB). Por defecto, s≤lo los campos no disponibles en Culturalia estßn a True.
  58.   ImportActors = False;
  59.   ImportCategory = False;
  60.   ImportComments = False;
  61.   ImportCountry = False;
  62.   ImportDescription = False;
  63.   ImportDirector = False;
  64.   ImportLength = True;
  65.   ImportLanguage = False;
  66.   ImportOriginalTitle = False;
  67.   ImportTranslatedTitle = False;
  68.   LeaveOriginalTitle = False; // True will get Translated Title, yet Original Title field will remain same
  69.   ImportPicture = False;
  70.   ImportLargePicture = False; // If set to False small pic will be imported
  71.   ImportRating = True;
  72.   ImportURL = False;
  73.   ImportYear = False;
  74.  
  75.   TitleMixedCase = False; // If true, each letter of each word of title begins with Uppercase. If false, the script transforms the titles in lowercase except first word
  76.  
  77.   ExternalPictures = False;
  78.     { True: Pictures will be stored as external files in the folder of the
  79.             catalog
  80.       False: Pictures will be stored inside the catalog (only for .amc files) }
  81.   ManualPictureSelect = False;
  82.     { True: If no Title Match found a picture selection window appears
  83.       False: Revert to IMDB picture }
  84.  
  85.   // Donde vamos a buscar en el caso de no haber introducido ni el tφtulo original ni el traducido en los campos correspondientes, sino en la ventana que
  86.   // se abre y nos lo solicita.
  87.   // 1-Titulo traducido, 2-Titulo original, 3-General
  88.   // What type of search in case we don't write the original title nor traslated one in the corresponding fields, we do it in the input box.
  89.   // 1-Translated title, 2-Original Title, 3-General
  90.   defdonde='1';
  91.  
  92. var
  93.   MovieName, Titulo: string;
  94.   MovieURL: string;
  95.   tmp: string;
  96.   donde: string;
  97.   Articles: array of string;
  98.   Index: Integer;
  99.  
  100. function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
  101. var
  102.   i: Integer;
  103. begin
  104.   result := -1;
  105.   if StartAt < 0 then
  106.     StartAt := 0;
  107.   for i := StartAt to List.Count-1 do
  108.     if Pos(Pattern, List.GetString(i)) <> 0 then
  109.     begin
  110.       result := i;
  111.       Break;
  112.     end;
  113. end;
  114.  
  115. procedure AnalyzePageIMDB(Address: string);
  116. var
  117.   Page: TStringList;
  118.   LineNr: Integer;
  119.   MovieURL: string;
  120. begin
  121.   Page := TStringList.Create;
  122.   Page.Text := GetPage(Address);
  123.   if pos('<title>IMDb', Page.Text) = 0 then
  124.   begin
  125.     AnalyzeMoviePageIMDB(Page)
  126.   end
  127.   else
  128.   begin
  129.     MovieURL := AddMoviesTitles(Page, '<b>Exact Matches</b>');
  130.     if MovieURL = '' then
  131.       MovieURL := AddMoviesTitles(Page, '<b>Partial Matches</b>');
  132.     if MovieURL = '' then
  133.       MovieURL := AddMoviesTitles(Page, '<b>Approximate Matches</b>');
  134.     if MovieURL <> '' then
  135.       AnalyzePageIMDB(MovieURL);
  136.   end;
  137.   Page.Free;
  138. end;
  139.  
  140. function FindValue(BeginTag, EndTag: string; Page: TStringList; var LineNr: Integer; var Line: string): string;
  141. var
  142.   BeginPos, EndPos: Integer;
  143.   Value: string;
  144. begin
  145.   Result := '';
  146.   Value := '';
  147.   BeginPos := Pos(BeginTag, Line);
  148.   if BeginPos > 0 then
  149.   begin
  150.     BeginPos := BeginPos + Length(BeginTag);
  151.     if BeginTag = EndTag then
  152.     begin
  153.       Delete(Line,1,BeginPos-1);
  154.       BeginPos := 1;
  155.     end;
  156.     EndPos := pos(EndTag, Line);
  157.     while ((EndPos = 0) and (LineNr < Page.Count-1 )) do
  158.     begin
  159.       Value := Value + copy(Line, BeginPos, Length(Line) - BeginPos);
  160.       // Next Line
  161.       BeginPos := 1;
  162.       LineNr := LineNr + 1;
  163.       Line := Page.GetString(LineNr);
  164.       if Value = '' then
  165.         Exit;
  166.       EndPos := Pos(EndTag, Line);
  167.     end;
  168.     Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  169.    end;
  170.   Result := Value;
  171. end;
  172.  
  173. procedure AnalyzeMoviePageIMDB(Page: TStringList);
  174. var
  175.   Line, Value, Value2, FullValue, OldOriginalTitle: string;
  176.   LineNr, Desc, i: Integer;
  177.   BeginPos, EndPos: Integer;
  178.   OldTitleParts, AllTitles: TStringList;
  179.   LongDescr: Boolean;
  180. begin
  181.   LongDescr := UseLongestDescIMDB;
  182.   if (LongDescr) and (Pos('<a href="plotsummary">', Page.Text) = 0) then
  183.     LongDescr := False;
  184.  
  185.   MovieURL := 'http://imdb.com/title/tt' + Copy(Page.Text, Pos('?pending&add=', Page.Text) + 17, 7);
  186.  
  187.   // URL
  188.   if ImportURL then
  189.     SetField(fieldURL, MovieURL);
  190.  
  191.   AllTitles := TStringList.Create;
  192.  
  193.   // Original Title & Year
  194.   if (ImportOriginalTitle) or (ImportYear) then
  195.   begin
  196.     LineNr := FindLine('<title>', Page, 0);
  197.     Line := Page.GetString(LineNr);
  198.     if LineNr > -1 then
  199.     begin
  200.       BeginPos := pos('<title>', Line);
  201.       if BeginPos > 0 then
  202.         BeginPos := BeginPos + 7;
  203.       EndPos := pos('(', Line);
  204.       if EndPos = 0 then
  205.         EndPos := Length(Line);
  206.       Value := copy(Line, BeginPos, EndPos - BeginPos - 1);
  207.       HTMLDecode(Value);
  208.       if ImportOriginalTitle then
  209.         OldOriginalTitle := GetField(fieldOriginalTitle);
  210.       if (ImportTranslatedTitle) and not (LeaveOriginalTitle) then
  211.         SetField(fieldOriginalTitle, Value);
  212.       BeginPos := pos('(', Line) + 1;
  213.       if BeginPos > 0 then
  214.       begin
  215.         EndPos := Pos('/I', Line);
  216.         if EndPos < BeginPos then
  217.           EndPos := pos(')', Line);
  218.         Value := copy(Line, BeginPos, EndPos - BeginPos);
  219.         if ImportYear then
  220.           SetField(fieldYear, Value);
  221.       end;
  222.     end;
  223.   end;
  224.  
  225.   // Translated Title
  226.   if ImportTranslatedTitle then
  227.   begin
  228.     OldTitleParts := TStringList.Create;
  229.     // Tokenize OldOriginalTitle while removing certain chars/common words ("the", "of")
  230.     Value := AnsiUpperCase(OldOriginalTitle);
  231.     Value := StringReplace(StringReplace(Value, ',', ' '), ':', ' ');
  232.     Value := StringReplace(StringReplace(Value, '(', ' '), ')', ' ');
  233.     Value := StringReplace(StringReplace(Value, 'OF', ' '), 'THE', ' ');
  234.     repeat
  235.       Value := StringReplace(Value, '  ', ' ');
  236.     until Pos('  ', Value) = 0;
  237.     Value := StringReplace(Trim(Value), ' ', ',');
  238.     // Value now contains the original title (comma-separated) that was filled in before running the script
  239.     Value2 := '';
  240.     for i := 1 to Length(Value) do
  241.     begin
  242.       if Pos(',', Copy(Value, i, 1)) = 0 then
  243.         Value2 := Value2 + Copy(Value, i, 1);
  244.       if (Pos(',', Copy(Value, i, 1)) = 1) or (i = Length(Value)) then
  245.       begin
  246.         OldTitleParts.Add(Value2); // put each comma-separated value from Value into a separate string in TitleParts
  247.         Value2 := '';
  248.       end;
  249.     end;
  250.     for i := 0 to OldTitleParts.Count - 1 do
  251.     // Begin comparing title parts (from the title originally filled in by moviedb owner) with
  252.     // the 'true' Original Title (extracted from IMDb) to see if it's a foreign title and needs a Translated Title
  253.     begin
  254.       if Pos(OldTitleParts.GetString(i), AnsiUpperCase(GetField(fieldOriginalTitle))) <= 0 then
  255.       begin // no match, must be a foreign title
  256.         LineNr := FindLine('Also Known As', Page, 0);
  257.         if LineNr > -1 then
  258.         begin
  259.           Line := Page.GetString(LineNr);
  260.           if Pos('Also Known As', Line) > 0 then
  261.           begin
  262.             BeginPos := Pos('Also Known As', Line) + 26;
  263.             Value := Copy(Line, BeginPos, Length(Line) - BeginPos - 4);
  264.             Value := StringReplace(Value, '<br>', '/ ');
  265.             HTMLDecode(Value);
  266.             SetField(fieldTranslatedTitle, Trim(Value));
  267.           end;
  268.         end;
  269.         Break;
  270.       end;
  271.     end;
  272.     OldTitleParts.Free;
  273.   end;
  274.  
  275.   // Rating
  276.   if ImportRating then
  277.   begin
  278.     LineNr := FindLine('User Rating:', Page, 0);
  279.     if LineNr > -1 then
  280.     begin
  281.       Line := Page.GetString(LineNr + 4);
  282.       if Pos('/10', Line) > 0 then
  283.       begin
  284.         BeginPos := pos('<b>', Line) + 3;
  285.         Value := IntToStr(Round(StrToInt(StrGet(Line, BeginPos), 0) + (StrToInt(StrGet(Line, BeginPos + 2), 0) / 10)));
  286.         SetField(fieldRating, Value);
  287.       end;
  288.     end;
  289.   end;
  290.  
  291.   // Language
  292.   LineNr := FindLine('Language:', Page, 0);
  293.   if LineNr > -1 then
  294.   begin
  295.     Line := Page.GetString(LineNr + 1);
  296.     BeginPos := pos('/">', Line) + 3;
  297.     EndPos := pos('</a>', Line);
  298.     if EndPos = 0 then
  299.       EndPos := Length(Line);
  300.     Value := copy(Line, BeginPos, EndPos - BeginPos);
  301.     if ImportLanguage then
  302.       SetField(fieldLanguages, Value);
  303.   end;
  304.  
  305.   if ImportPicture then
  306.     GetMoviePicture(Value, Page, AllTitles);
  307.   AllTitles.Free;
  308.  
  309.   // Director
  310.   if ImportDirector then
  311.   begin
  312.     LineNr := FindLine('Directed by', Page, 0);
  313.     if LineNr > -1 then
  314.     begin
  315.       FullValue := '';
  316.       Line := Page.GetString(LineNr + 1);
  317.       repeat
  318.         BeginPos := pos('">', Line) + 2;
  319.         EndPos := pos('</a>', Line);
  320.         Value := copy(Line, BeginPos, EndPos - BeginPos);
  321.         if (Value <> '(more)') and (Value <> '') then
  322.         begin
  323.           if FullValue <> '' then
  324.             FullValue := FullValue + ', ';
  325.           FullValue := FullValue + Value;
  326.         end;
  327.         Delete(Line, 1, EndPos);
  328.       until Pos('</a>', Line) = 0;
  329.       HTMLDecode(FullValue);
  330.       SetField(fieldDirector, FullValue);
  331.     end;
  332.   end;
  333.  
  334.   // Actors
  335.   if ImportActors then
  336.   begin
  337.     LineNr := FindLine('ast overview', Page, 0);
  338.     if LineNr = -1 then
  339.       LineNr := FindLine('redited cast', Page, 0);
  340.     if LineNr > -1 then
  341.     begin
  342.       FullValue := '';
  343.       Line := Page.GetString(LineNr);
  344.       repeat
  345.         BeginPos := Pos('<td valign="top">', Line);
  346.         if BeginPos > 0 then
  347.         begin
  348.           Delete(Line, 1, BeginPos);
  349.           Line := copy(Line, 25, Length(Line));
  350.           BeginPos := pos('">', Line) + 2;
  351.           EndPos := pos('</a>', Line);
  352.           if EndPos = 0 then
  353.             EndPos := Pos('</td>', Line);
  354.           Value := copy(Line, BeginPos, EndPos - BeginPos);
  355.           if (Value <> '(more)') and (Value <> '') then
  356.           begin
  357.             BeginPos := pos('.... </td><td valign="top">', Line);
  358.             if BeginPos > 0 then
  359.             begin
  360.               EndPos := pos('</td></tr>', Line);
  361.               BeginPos := BeginPos + 27;
  362.               Value2 := copy(Line, BeginPos, EndPos - BeginPos);
  363.               if Value2 <> '' then
  364.               begin
  365.                 Value := Value + ' (as ' + Value2 + ')';
  366.               end;
  367.             end;
  368.             if FullValue <> '' then
  369.               FullValue := FullValue + ', ';
  370.             FullValue := FullValue + Value;
  371.           end;
  372.           EndPos := Pos('</td></tr>', Line);
  373.           Delete(Line, 1, EndPos);
  374.         end else
  375.         begin
  376.           Line := '';
  377.         end;
  378.       until Line = '';
  379.       HTMLDecode(FullValue);
  380.       SetField(fieldActors, FullValue);
  381.     end;
  382.   end;
  383.  
  384.   // Country
  385.   if ImportCountry then
  386.   begin
  387.     LineNr := FindLine('Country:', Page, 0);
  388.     if LineNr > -1 then
  389.     begin
  390.       Line := Page.GetString(LineNr + 1);
  391.       BeginPos := pos('/">', Line) + 3;
  392.       EndPos := pos('</a>', Line);
  393.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  394.       HTMLDecode(Value);
  395.       SetField(fieldCountry, Value);
  396.     end;
  397.   end;
  398.  
  399.   // Category
  400.   if ImportCategory then
  401.   begin
  402.     LineNr := FindLine('Genre:', Page, 0);
  403.     if LineNr > -1 then
  404.     begin
  405.       Line := Page.GetString(LineNr + 1);
  406.       BeginPos := pos('/">', Line) + 3;
  407.       EndPos := pos('</a>', Line);
  408.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  409.       HTMLDecode(Value);
  410.       SetField(fieldCategory, Value);
  411.     end;
  412.   end;
  413.  
  414.   //Description
  415.   if ImportDescription then
  416.   begin
  417.     LineNr := FindLine('Plot Summary:', Page, 0);
  418.     if LineNr < 1 then
  419.       LineNr := FindLine('Plot Outline:', Page, 0);
  420.     if LineNr > -1 then
  421.     begin
  422.       Line := Page.GetString(LineNr);
  423.       BeginPos := pos('</b>', Line) + 5;
  424.       EndPos := pos('<a href="/rg/', Line);
  425.       if EndPos < 1 then
  426.       begin
  427.         Line := Line + Page.GetString(LineNr+1);
  428.         EndPos := pos('<a href="/rg/', Line);
  429.         if EndPos < 1 then
  430.           EndPos := pos('<br><br>', Line);
  431.         if EndPos < 1 then
  432.           EndPos := Length(Line);
  433.       end;
  434.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  435.       HTMLDecode(Value);
  436.       HTMLRemoveTags(Value);
  437.       if UseLongestDescIMDB then
  438.         SetField(fieldDescription, GetDescriptions(MovieURL + 'plotsummary'))
  439.       else
  440.         SetField(fieldDescription, Value);
  441.     end;
  442.   end;
  443.  
  444.   // Comments
  445.   if ImportComments then
  446.   begin
  447.     LineNr := FindLine('<b>Summary:</b>', Page, 0);
  448.     if LineNr > -1 then
  449.     begin
  450.       Value := '';
  451.       repeat
  452.         LineNr := LineNr + 1;
  453.         Line := Page.GetString(LineNr);
  454.         EndPos := Pos('</blockquote>', Line);
  455.         if EndPos = 0 then
  456.           EndPos := Length(Line)
  457.         else
  458.           EndPos := EndPos - 1;
  459.         Value := Value + Copy(Line, 1, EndPos) + ' ';
  460.       until Pos('</blockquote>', Line) > 0;
  461.       HTMLDecode(Value);
  462.       Value := StringReplace(Value, '<br>', #13#10);
  463.       Value := StringReplace(Value, #13#10+' ', #13#10);
  464.       SetField(fieldComments, Value);
  465.     end;
  466.   end;
  467.  
  468.   // Length
  469.   if ImportLength then
  470.   begin
  471.     LineNr := FindLine('Runtime:', Page, 0);
  472.     if LineNr > -1 then
  473.     begin
  474.       Line := Page.GetString(LineNr + 1);
  475.       EndPos := pos(' min', Line);
  476.       if EndPos = 0 then
  477.         EndPos := pos('  /', Line);
  478.       if EndPos = 0 then
  479.         EndPos := Length(Line);
  480.       if Pos(':', Line) < EndPos then
  481.         BeginPos := Pos(':', Line) + 1
  482.       else
  483.         BeginPos := 1;
  484.       Value := copy(Line, BeginPos, EndPos - BeginPos);
  485.       SetField(fieldLength, Value);
  486.     end;
  487.   end;
  488.  
  489.   DisplayResults;
  490. end;
  491.  
  492. procedure GetMoviePicture(Language: string; Page, AllTitles: TStringList);
  493. var
  494.   Line, Value, Value2, Aka, PictureAddress: string;
  495.   AmazonPage: TStringList;
  496.   FoundOnAmazon, PickTreeSelected, PictureAvailable: Boolean;
  497.   TitleRef, ImgRef, NoImage: string;
  498.   LineNr, BeginPos, EndPos, PickTreeCount, ParagraphIndex, Index, TitleLine, LastMatch: Integer;
  499. begin
  500.   FoundOnAmazon := False;
  501.  
  502.   if ImportLargePicture then
  503.   begin
  504.     // Find Alternate Titles for Movies which are not in English
  505.     Aka := '';
  506.     if Language <> 'English' Then
  507.     begin
  508.       LineNr:= FindLine('Also Known As',Page,0);
  509.       EndPos:=0;
  510.       if LineNr > -1 then
  511.       begin
  512.         Line := Page.GetString(LineNr);
  513.         repeat
  514.           Aka:=FindValue('<br>','<br>',Page,LineNr,Line);
  515.           if Aka <> '' then
  516.           begin
  517.             BeginPos:=1;
  518.             EndPos:=Pos('(',Line);
  519.             if EndPos = 0 then
  520.               EndPos := Length(Aka);
  521.             Value := copy(Aka, BeginPos, EndPos - BeginPos - 1);
  522.             Value:=TransFormIMDBTitle(Value);
  523.             AllTitles.Add(Value);
  524.           end;
  525.         until (Pos('</td>', Line) > 0) or (Pos('Runtime', Line) > 0) or (Pos('MPAA', Line) > 0 ) or (Pos('Country', Line) > 0) or (Pos('Certification', Line) > 0);
  526.       end;
  527.     end;
  528.  
  529.     TitleRef:='dvd>';
  530.     ImgRef:='dvd><img';
  531.     NoImage:='/icons/dvd-no-image.gif';
  532.     LineNr := FindLine('title="DVD available at Amazon.com"', Page, 0);
  533.     if LineNr = -1 then
  534.     begin
  535.       LineNr := FindLine('title="VHS available at Amazon.com"', Page, 0);
  536.       if LineNr > -1 then
  537.       begin
  538.         TitleRef:='video>';
  539.         ImgRef:='video><img';
  540.         NoImage:='/icons/video-no-image.gif';
  541.       end;
  542.     end;
  543.  
  544.     if LineNr > -1 then
  545.     begin
  546.       Line := Page.GetString(LineNr);
  547.       if(TitleRef='dvd>') then
  548.       begin
  549.         EndPos := pos('title="DVD', Line);
  550.         BeginPos := pos('title="VHS', Line);
  551.         while (BeginPos > 0) and (BeginPos<EndPos) do
  552.         begin
  553.           Delete(Line, 1, BeginPos+1);
  554.           BeginPos := pos('title="VHS', Line);
  555.         end;
  556.       end;
  557.       BeginPos := Pos('href="', Line) + 5;
  558.       Delete(Line, 1, BeginPos);
  559.       EndPos := Pos('"', Line);
  560.       Value := Copy(Line, 1, EndPos - 1);
  561.       AmazonPage := TStringList.Create;
  562.       AmazonPage.Text := GetPage('http://us.imdb.com' + Value);
  563.  
  564.       // Original Title
  565.       Value2 := AllTitles.GetString(0);
  566.       Value2 := TransFormIMDBTitle(Value2);
  567.  
  568.       PickTreeClear;
  569.       PickTreeCount := 0;
  570.       PickTreeAdd('Available Titles for matching a picture to: ' + Value2, '');
  571.  
  572.       ParagraphIndex := 1;
  573.       LineNr := 0;
  574.       LastMatch := -1;
  575.       TitleLine := -1;
  576.       repeat
  577.         LineNr := FindLine('<b>'+IntToStr(ParagraphIndex)+'.', AmazonPage, LineNr);
  578.  
  579.         if LineNr > -1 then
  580.         begin
  581.           TitleLine:=LineNr;
  582.           Value:='';
  583.           PictureAvailable:=False;
  584.           repeat
  585.             TitleLine:=TitleLine +1;
  586.             Line:= AmazonPage.GetString(TitleLine);
  587.             BeginPos:=0;
  588.             if Pos(TitleRef,Line) > 0 then
  589.             begin
  590.               if Pos(ImgRef,Line) = 0 then
  591.               begin
  592.                 for Index:=0 to AllTitles.Count -1 do
  593.                 begin
  594.                   Value2:=AllTitles.GetString(Index);
  595.                   BeginPos:=Pos(Value2,Line);
  596.                   if BeginPos > 0 then
  597.                     Break;
  598.                 end;
  599.                 // Match not found
  600.                 if BeginPos = 0 then
  601.                 begin
  602.                   BeginPos:=Pos(TitleRef,Line)+Length(TitleRef);
  603.                   EndPos:=Pos('</a>',Line);
  604.                   Value:=Copy(Line,BeginPos,EndPos-BeginPos);
  605.                 end;
  606.               end
  607.               else
  608.               begin
  609.                 PictureAvailable:=(Pos(NoImage,Line) = 0);
  610.                 PictureAddress:=IntToStr(TitleLine);
  611.               end;
  612.             end;
  613.             if BeginPos > 0 then
  614.               Break;
  615.           until (Pos('</table>',Line ) > 0);
  616.  
  617.           // Try to Find a Title Match
  618.           if Pos(Value2,Line) > 0 then
  619.           begin
  620.             // Compare Current Title to Original
  621.             BeginPos := Pos(TitleRef, Line) + Length(TitleRef) -1;
  622.             Delete(Line, 1, BeginPos);
  623.             EndPos:= Pos('(',Line);
  624.             if EndPos = 0 Then
  625.               EndPos := Pos('</a>', Line);
  626.             Value := Copy(Line, 1, EndPos - 1);
  627.             Value:= Trim(Value);
  628.             if Value = Value2 then
  629.             begin
  630.               if PictureAvailable then
  631.                 LastMatch:=LineNr;
  632.                 //Break
  633.             end;
  634.           end;
  635.           if PictureAvailable then
  636.           begin
  637.             PickTreeAdd(Value,PictureAddress);
  638.             PickTreeCount:=PickTreeCount+1;
  639.           end;
  640.         end;
  641.         ParagraphIndex:=ParagraphIndex+1;
  642.       until (LineNr = -1);
  643.       LineNr:=LastMatch;
  644.       if (LineNr = -1) then
  645.       begin
  646.         // Handle Amazon Page Redirection(s)
  647.         LineNr:= FindLine('You clicked on this item',AmazonPage,0);
  648.         if (LineNr = -1) then
  649.           LineNr:=FindLine('Customers who bought',AmazonPage,0);
  650.         // Display the Picture Selection Window
  651.         if (LineNr = -1) and ManualPictureSelect and (PickTreeCount > 0) then
  652.         begin
  653.           PickTreeSelected:=PickTreeExec(PictureAddress);
  654.           if PickTreeSelected then
  655.             LineNr:=StrToInt(PictureAddress,0);
  656.         end;
  657.         if (LineNr > -1 ) then
  658.         begin
  659.           LineNr := FindLine('src="http://images.amazon.com/images/P/',AmazonPage, LineNr);
  660.           if not PickTreeSelected then
  661.             TitleLine:= FindLine('/exec/obidos/ASIN/',AmazonPage, 0);
  662.           if (LineNr > TitleLine) then
  663.             LineNr:=-1;
  664.           if LineNr > -1 then
  665.           begin
  666.             Line := AmazonPage.GetString(LineNr);
  667.             BeginPos := Pos('src="http://images.amazon.com/images/P/', Line) + 4;
  668.             Delete(Line, 1, BeginPos);
  669.             EndPos := Pos('"', Line);
  670.             Value := Copy(Line, 1, EndPos - 1);
  671.             Value := StringReplace(Value, 'TZZZZZZZ', 'LZZZZZZZ');
  672.             Value := StringReplace(Value, 'THUMBZZZ', 'LZZZZZZZ');
  673.             GetPicture(Value, ExternalPictures);
  674.             FoundOnAmazon := True;
  675.           end;
  676.         end;
  677.       end
  678.       else
  679.       begin
  680.         LineNr := FindLine('http://images.amazon.com/images/P/', AmazonPage, LineNr);
  681.         if LineNr < TitleLine then
  682.         begin
  683.           Line := AmazonPage.GetString(LineNr);
  684.           BeginPos := Pos('src="', Line) + 4;
  685.           Delete(Line, 1, BeginPos);
  686.           EndPos := Pos('"', Line);
  687.           Value := Copy(Line, 1, EndPos - 1);
  688.           Value := StringReplace(Value, 'THUMBZZZ', 'LZZZZZZZ');
  689.           GetPicture(Value, ExternalPictures);
  690.           FoundOnAmazon := True;
  691.         end;
  692.       end;
  693.       AmazonPage.Free;
  694.     end;
  695.   end; // if ImportLargePicture
  696.  
  697.   if not FoundOnAmazon then
  698.   begin
  699.     {  not found on Amazon, so taking what's available directly on IMDB.  }
  700.     LineNr := FindLine('<img border="0" alt="cover"', Page, 0);
  701.     if LineNr > -1 then
  702.     begin
  703.       Line := Page.GetString(LineNr);
  704.       BeginPos := pos('src="', Line) + 4;
  705.       Delete(Line, 1, BeginPos);
  706.       EndPos := pos('"', Line);
  707.       Value := copy(Line, 1, EndPos - 1);
  708.       GetPicture(Value, ExternalPictures);
  709.     end;
  710.   end;
  711. end;
  712.  
  713. function TransformTitle(Title: string): string;
  714. var
  715.   BeginPos, EndPos: Integer;
  716.   Value: string;
  717.   Words: array of string;
  718.   Articles: array of string;
  719.   Replace,Original: string;
  720.   Index, CommaCount: Integer;
  721. Begin
  722.   // Original Title
  723.   Result:=Title;
  724.  
  725.   Setarraylength(Words,11);
  726.   Words[0]:=' In ';
  727.   Words[1]:=' On ';
  728.   Words[2]:=' Of ';
  729.   Words[3]:=' As ';
  730.   Words[4]:=' The ';
  731.   Words[5]:=' At ';
  732.   Words[6]:=' And A ';
  733.   Words[7]:=' And ';
  734.   Words[8]:=' An ';
  735.   Words[9]:=' To ';
  736.   Words[10]:=' For ';
  737.  
  738.   SetArrayLength(Articles,35);
  739.   Articles[0]:=' The';
  740.   Articles[1]:=' a';
  741.   Articles[2]:=' An';
  742.   Articles[3]:=' Le';
  743.   Articles[4]:=' L''';
  744.   Articles[5]:=' Les';
  745.   Articles[6]:=' Der';
  746.   Articles[7]:=' Das';
  747.   Articles[8]:=' Die';
  748.   Articles[9]:=' Des';
  749.   Articles[10]:=' Dem';
  750.   Articles[11]:=' Den';
  751.   Articles[12]:=' Ein';
  752.   Articles[13]:=' Eine';
  753.   Articles[14]:=' Einen';
  754.   Articles[15]:=' Einer';
  755.   Articles[16]:=' Eines';
  756.   Articles[17]:=' Einem';
  757.   Articles[18]:=' Il';
  758.   Articles[19]:=' Lo';
  759.   Articles[20]:=' La';
  760.   Articles[21]:=' I';
  761.   Articles[22]:=' Gli';
  762.   Articles[23]:=' Le';
  763.   Articles[24]:=' Uno';
  764.   Articles[25]:=' Una';
  765.   Articles[26]:=' Un''';
  766.   Articles[27]:=' O';
  767.   Articles[28]:=' Os';
  768.   Articles[29]:=' As';
  769.   Articles[30]:=' El';
  770.   Articles[31]:=' Los';
  771.   Articles[32]:=' Las';
  772.   Articles[33]:=' Unos';
  773.   Articles[34]:=' Unas';
  774.  
  775.   // Count the Comma in The Title
  776.   CommaCount := 0;
  777.   EndPos := 0;
  778.   Value := Title;
  779.   repeat
  780.      BeginPos := Pos(',', Value);
  781.      if BeginPos > 0 then
  782.      begin
  783.        Delete(Value, 1, BeginPos);
  784.        CommaCount := CommaCount + 1;
  785.        EndPos := EndPos + BeginPos;
  786.      end;
  787.   until( Pos(',',Value) = 0);
  788.  
  789.   // Compare the Article to a list of known ones
  790.   for Index := 0 to 34 do
  791.   begin
  792.     if Pos(Articles[Index], Value) <> 0 then
  793.     begin
  794.        CommaCount := 1;
  795.        BeginPos := EndPos;
  796.        Break;
  797.     end;
  798.   end;
  799.  
  800.   if (BeginPos > 0) and (CommaCount = 1) then
  801.   begin
  802.     Value := Copy(Title, BeginPos + 1, Length(Title));
  803.     Value := Trim(Value);
  804.     Result := Value + ' ' + Copy(Title, 1, BeginPos - 1);
  805.   end;
  806.  
  807.   BeginPos := Pos(': ', Result);
  808.   if BeginPos > 0 then
  809.     Result := StringReplace(Result, ': ', ' - ');
  810.  
  811.   Result := AnsiMixedCase(Result, ' ');
  812.  
  813.   for Index := 0 to 10 do
  814.   begin
  815.     if Pos(Words[Index],Result) <> 0 then
  816.     begin
  817.       Original := Words[Index];
  818.       Replace := AnsiLowerCase(Original);
  819.       Result := StringReplace(Result, Original, Replace);
  820.     end;
  821.   end;
  822.  
  823.   Result := StringReplace(Result, ' - the ', ' - The ');
  824.   Result := Trim(Result);
  825. end;
  826.  
  827. function GetDescriptions(Address: string): string;
  828. var
  829.   Line, Value: string;
  830.   LineNr: Integer;
  831.   BeginPos, EndPos,Longest: Integer;
  832.   Page: TStringList;
  833. begin
  834.   Result := '';
  835.   Longest := 0;
  836.   Page := TStringList.Create;
  837.   Page.Text := GetPage(Address);
  838.   LineNr := FindLine('<p class="plotpar">', Page, 0);
  839.   while LineNr > -1 do
  840.   begin
  841.     Value := '';
  842.     repeat
  843.       Line := Page.GetString(LineNr);
  844.       BeginPos := pos('"plotpar">', Line);
  845.       if BeginPos > 0 then
  846.         BeginPos := BeginPos + 10
  847.       else
  848.         BeginPos := 1;
  849.       EndPos := pos('</p>', Line);
  850.       if EndPos < 1 then
  851.         EndPos := Length(Line) + 1;
  852.       if Value <> '' then
  853.         Value := Value + ' ';
  854.       Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
  855.       LineNr := LineNr + 1;
  856.     until (pos('</p>', Line) > 0) or (LineNr = Page.Count);
  857.     HTMLDecode(Value);
  858.     HTMLRemoveTags(Value);
  859.     PickListAdd(Value);
  860.  
  861.     if Length(Value) > Longest then
  862.     begin
  863.       Result := Value;
  864.       Longest := Length(Value);
  865.     end;
  866.  
  867.     LineNr := FindLine('<p class="plotpar">', Page, LineNr);
  868.   end;
  869.   Page.Free;
  870. end;
  871.  
  872. function AddMoviesTitles(Page: TStringList; Tag: string): string;
  873. var
  874.   Line: string;
  875.   LineNr: Integer;
  876.   StartPos: Integer;
  877. begin
  878.   Result := '';
  879.   LineNr := FindLine(tag, Page, 0);
  880.   if LineNr > -1 then
  881.   begin
  882.     Line := Page.GetString(LineNr);
  883.     HTMLRemoveTags(Line);
  884.     PickTreeAdd(Trim(Line), '');
  885.     LineNr := LineNr + 5;
  886.     Line := Page.GetString(LineNr);
  887.     StartPos := pos('href="', Line) + 5;
  888.     Delete(Line, 1, StartPos);
  889.     Result := Copy(Line, 1, pos('">', Line) - 1);
  890.   end;
  891. end;
  892.  
  893. procedure AnalyzePageCulturalia(Address: string);
  894. var
  895.   Page: TStringList;
  896.   LineNr: Integer;
  897.   Code, Title, TitleOrig, Year: string;
  898. begin
  899.   Page := TStringList.Create;
  900.   Page.Text := GetPage(Address);
  901.   if Pos('No se ha encontrado ning·n artφculo por tφtulo', Page.Text) > 0 then
  902.   begin
  903.     ShowMessage('No se ha encontrado ninguna coincidencia por tφtulo');
  904.   end else
  905.   begin
  906.     PickTreeClear;
  907.     LineNr := 1;
  908.     Page.Text := StringReplace(Page.Text, '<br>', #13#10);
  909.     PickTreeAdd('Resultados mßs probables de la b·squeda:', '');
  910.     while LineNr + 3 < Page.Count do
  911.     begin
  912.       Code := GetValueAfter(Page.GetString(LineNr), 'Codigo = ');
  913.       Title := GetValueAfter(Page.GetString(LineNr+1), 'Titulo = ');
  914.       TitleOrig := GetValueAfter(Page.GetString(LineNr+2), 'Titulo original = ');
  915.       Year := GetValueAfter(Page.GetString(LineNr+3), 'A±o = ');
  916.       PickTreeAdd(Title + ' (' + TitleOrig + '), ' + Year, BaseURLCulturalia + '?catalogo=1&codigo=' + Code);
  917.       LineNr := LineNr + 5;
  918.     end;
  919.     Page.Free;
  920.     if PickTreeExec(Address) then
  921.       AnalyzeMoviePageCulturalia(Address);
  922.   end;
  923. end;
  924.  
  925. procedure AnalyzeMoviePageCulturalia(Address: string);
  926. var
  927.   Page: TStringList;
  928.   Comments: string;
  929.   strTitle: string;
  930.   strSinopsis: string;
  931.   Line: string;
  932.   LineNr: Integer;
  933.   tmp: string;
  934. begin
  935.   Page := TStringList.Create;
  936.   Page.Text := StringReplace(GetPage(Address), '<br><br>', #13#10);
  937.   Page.Text := StringReplace(Page.Text, '<br>', #13#10);
  938.   strTitle := GetValueAfter(Page.GetString(1), 'Titulo = ');
  939.   if copy(strTitle, Length(strTitle), Length(strTitle)) = '.' then
  940.   begin
  941.     tmp := Copy(strTitle, 1, Length(strTitle) -1);
  942.   end else
  943.   begin
  944.     tmp := strTitle;
  945.   end;
  946.   SetField(fieldTranslatedTitle, TransformTitle(tmp));
  947.   tmp := GetValueAfter(Page.GetString(2), 'Titulo original = ');
  948.   SetField(fieldOriginalTitle, TransformTitle(tmp));
  949.   SetField(fieldYear, GetValueAfter(Page.GetString(3), 'A±o = '));
  950.   SetField(fieldCategory, GetValueAfter(Page.GetString(4), 'Genero = '));
  951.   SetField(fieldCountry, GetValueAfter(Page.GetString(5), 'Nacion = '));
  952.   SetField(fieldDirector, GetValueAfter(Page.GetString(6), 'Director = '));
  953.   SetField(fieldActors, GetValueAfter(Page.GetString(7), 'Actores = '));
  954.   SetField(fieldProducer, GetValueAfter(Page.GetString(8), 'Productor = '));
  955.   Comments := 'Gui≤n: ' + GetValueAfter(Page.GetString(9), 'Guion = ');
  956.   Comments := Comments + #13#10 + 'Fotografφa: ' + GetValueAfter(Page.GetString(10), 'Fotografia = ');
  957.   Comments := Comments + #13#10 + 'M·sica: ' + GetValueAfter(Page.GetString(11), 'Musica = ');
  958.   SetField(fieldComments, Comments);
  959.   LineNr := FindLine('Sinopsis = ', Page, 0);
  960.   Line := Page.GetString(LineNr);
  961.   strSinopsis := GetValueAfter(Line, 'Sinopsis = ');
  962.   LineNr := LineNr + 1;
  963.   Line := Page.GetString(LineNr);
  964.   while pos('URL = ', Line) = 0 do
  965.   begin
  966.     strSinopsis := strSinopsis + #13#10 + Line;
  967.     LineNr := LineNr + 1;
  968.     Line := Page.GetString(LineNr);
  969.   end
  970.   HTMLRemoveTags(strSinopsis);
  971.   SetField(fieldDescription, StringReplace(StringReplace(strSinopsis, 'ô', '"'), 'ö', '"'));
  972.   LineNr := FindLine('URL = ', Page, 0);
  973.   if LineNr <> -1 then
  974.     SetField(fieldURL, GetValueAfter(Page.GetString(LineNr), 'URL = '));
  975.   LineNr := FindLine('Imagen = ', Page, 0);
  976.   if LineNr <> -1 then
  977.     GetPicture(GetValueAfter(Page.GetString(LineNr), 'Imagen = '), False);
  978.   Page.Free;
  979. end;
  980.  
  981. function GetValueAfter(Line, Identifier: string): string;
  982. begin
  983.   if Pos(Identifier, Line) = 1 then
  984.     Result := Copy(Line, Length(Identifier)+1, Length(Line))
  985.   else
  986.     Result := '';
  987. end;
  988.  
  989. begin
  990.   SetArrayLength(Articles,11);
  991.   Articles[0]:='Lo ';
  992.   Articles[1]:='La ';
  993.   Articles[2]:='Le ';
  994.   Articles[3]:='Uno ';
  995.   Articles[4]:='Una ';
  996.   Articles[5]:='Un ';
  997.   Articles[6]:='El ';
  998.   Articles[7]:='Los ';
  999.   Articles[8]:='Las ';
  1000.   Articles[9]:='Unos ';
  1001.   Articles[10]:='Unas ';
  1002.  
  1003. if CheckVersion(3,4,0) then
  1004.    begin
  1005.     MovieName := GetField(fieldOriginalTitle);
  1006.      donde := '&donde=2';
  1007.      if MovieName = '' then
  1008.       begin
  1009.        MovieName := GetField (fieldTranslatedTitle);
  1010.        donde := '&donde=1';
  1011.       end
  1012.      if MovieName = '' then
  1013.       begin
  1014.        Input('Importar de Culturalia', 'Introduce el Titulo de la Pelicula:', MovieName);
  1015.        donde := '&donde=' + defdonde;
  1016.       end
  1017.     If MovieName <> '' then
  1018.       begin
  1019.         // Eliminate spanish article if exists
  1020.         for Index := 0 to 10 do
  1021.         begin
  1022.          if Pos(Articles[Index], MovieName) <> 0 then
  1023.          MovieName := copy(MovieName, length(Articles[Index]), length(MovieName));
  1024.         end;
  1025.  
  1026.         // Eliminate point(s) at final of MovieName before search
  1027.         tmp := MovieName;
  1028.         if Copy(tmp, Length(tmp), Length(tmp)) = '.' then
  1029.           MovieName := Copy(tmp, 1, Length(tmp) -1);
  1030.         AnalyzePageCulturalia(BaseURLCulturalia + '?catalogo=1&texto=' + UrlEncode(MovieName) + donde);
  1031.         AnalyzePageIMDB('http://us.imdb.com/Tsearch?title='+UrlEncode(GetField(fieldOriginalTitle)));
  1032.         DisplayResults;
  1033.       end;
  1034.    end else
  1035.      ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
  1036. end.
  1037.